package com.company.ui;

import com.company.MyLinkList;
import com.company.Student;
import com.company.StudentTable;

import javax.swing.*;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

/**
 * Created by v on 16-12-20.
 */
public class TableUI extends JPanel{
    private MyLinkList<StudentTable> tables;
    private StudentUI studentUI;
    private ListPanel listPanel;

    private JButton jbtAdd=new JButton("新增一个学生档案表");
    private JButton jbtRemove=new JButton("删除一个学生档案表");
    private JButton jbtRename=new JButton("更改一个学生档案表的名字");

    public TableUI(StudentUI studentUI,MyLinkList<StudentTable> tables){
        this.tables=tables;
        this.studentUI=studentUI;
        listPanel=new ListPanel();

        JPanel panel=new JPanel();
        panel.add(jbtAdd);
        panel.add(jbtRemove);
        panel.add(jbtRename);

        setLayout(new BorderLayout());
        add(listPanel,BorderLayout.CENTER);
        add(panel,BorderLayout.NORTH);

        jbtAdd.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                try{
                    String name=JOptionPane.showInputDialog(
                            null,"请输入档案表的名字");

                    StudentTable table=new StudentTable(name);
                    tables.add(table);
                    listPanel.rebuild();
                }catch (Exception ex){

                }
            }
        });

        jbtRemove.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if(listPanel.target==-1
                        ||listPanel.target>=tables.size()){
                    return;
                }

                tables.remove(listPanel.target);
                studentUI.rebuild();
                listPanel.rebuild();
            }
        });

        jbtRename.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if(listPanel.target==-1
                        ||listPanel.target>=tables.size()){
                    return;
                }

                try{
                    String name=JOptionPane.showInputDialog(null
                            ,"请输入你要更改的名字");

                    tables.get(listPanel.target).setTableName(name);
                    listPanel.rebuild();
                }catch (Exception ex){

                }
            }
        });
    }

    //滚动Panel
    private class ListPanel extends JScrollPane {
        private JList<Student> myList;
        private ListModel<Student> listModel;

        //被选中的目标
        private int target=-1;

        public ListPanel(){
            listModel=new DefaultComboBoxModel(tables.toArray());
            myList=new JList<>();
            myList.setFont(new Font("微软雅黑",0,20));
            myList.setModel(listModel);
            this.setViewportView(myList);

            //多选框监听器，对选中单位放入到target
            myList.addListSelectionListener(new ListSelectionListener() {
                @Override
                public void valueChanged(ListSelectionEvent e) {
                    target=myList.getSelectedIndex();
                    studentUI.setStudents(tables.get(target).getList());
                }
            });
        }

        public void rebuild(){
            listModel=new DefaultComboBoxModel(tables.toArray());
            myList.setModel(listModel);
        }
    }
}
